﻿namespace nBayes
{
    using System.Linq;

    internal class MemoryIndex : Index
    {
        internal SerializableDictionary<string, int> table = new IndexTable<string, int>();

        private static object synclock = new object();

        public MemoryIndex()
        {
        }

        public override int EntryCount
        {
            get
            {
                lock (synclock)
                    return table.Values.Sum();
            }
        }

        public override void Add(Entry document)
        {
            lock (synclock)
                foreach (string token in document)
                {
                    if (table.ContainsKey(token))
                    {
                        table[token]++;
                    }
                    else
                    {
                        table.Add(token, 1);
                    }
                }
        }

        public override int GetTokenCount(string token)
        {
            return this.table.ContainsKey(token) ? this.table[token] : 0;
        }
    }
}
